package com.yujiahao.bigdata.streaming

import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}

object Stream_Source_Dir {
  def main(args: Array[String]): Unit = {
    //TODO SparkStreaming环境
    val  conf = new SparkConf().setMaster("local[*]").setAppName("WordCount")
    //StreamingContext的构造方法第一个参数是配置文件，第二个参数表示数据采集的周期（微批次）
    val ssc: StreamingContext = new StreamingContext(conf, Seconds(3))

    //从文件夹中测试
    val dirDS: DStream[String] = ssc.textFileStream("input")

    //还是测试的是WordCount
    val wordDS: DStream[String] = dirDS.flatMap(_.split(" "))
    val wordToOne: DStream[(String, Int)] = wordDS.map((_, 1))
    val wordToCountDS: DStream[(String, Int)] = wordToOne.reduceByKey(_ + _)
   //流式数据过来一定要处理，不然会报错
    wordToCountDS.print()


    //启动采集器
    // No output operations registered, so nothing to execute
    ssc.start()
    //Driver等待采集器的结束，否则，当前Driver处于阻塞状态
    ssc.awaitTermination()

  }

}
